package com.joplus.housekeeper.xmpp;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;

import com.hyphenate.chatuidemo.DemoApplication;
import com.hyphenate.chatuidemo.ui.SplashActivity;
import com.joplus.housekeeper.utils.T;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.XMPPConnection;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * 服务器连接监听
 *
 * @author: laohu on 2017/1/18
 * @site: http://ittiger.cn
 */
public class SmackConnectionListener implements ConnectionListener {

    @Override
    public void connected(XMPPConnection connection) {

        Log.e("connect", "connection connected");
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {

        Log.e("connect", "connection authenticated");
    }

    @Override
    public void connectionClosed() {

        Log.e("connect", "connection connectionClosed");

       /* Observable.just("您的账号在其他设备上登录，您已被迫下线").subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Context context = DemoApplication.getInstance();
                T.showLong(context, s);
                Intent intent = new Intent(context, SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent);
            }
        });*/
    }

    @Override
    public void connectionClosedOnError(Exception e) {

        Log.e("connect", "connectionClosedOnError");
        Observable.just("您的账号在其他设备上登录，您已被迫下线").subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Context context = DemoApplication.getInstance();
                T.showLong(context, s);
                Intent intent = new Intent(context, SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent);
            }
        });


    }

    @Override
    public void reconnectingIn(int seconds) {

        Log.e("connect", "connection reconnectingIn " + seconds + " second");
    }

    @Override
    public void reconnectionFailed(Exception e) {

        Log.e("connect", "reconnectionFailed");
    }

    @Override
    public void reconnectionSuccessful() {

        Log.e("connect", "reconnectionSuccessful");
    }
}
